<?php
if(!defined('BASEPATH'))exit('No direct script access allowed');
class News extends CI_Controller{
    function News()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('news_model');
        $this->load->model('counter_model');
    }
    function index()
    {
        $per_page = 10;
        $total = $this->counter_model->news_count();
        $offset=0+$this->uri->segment(4);
        $config['base_url'] = base_url() . 'admin/news/index/';
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '4';
        $this->pagination->initialize($config);
        $data['news']=$this->news_model->get_all_news($per_page, $offset);
        $this->load->view('admin/admin_news',$data);
    }
    function news_insert()
    {
        if ($this->input->post('submitnews')) {
            $this->form_validation->set_rules('newstitle', 'newstitle', 'required|xss_clean|is_unique[news.title]');
            $this->form_validation->set_rules('newsdescription', 'newsdescription', 'required|xss_clean');
            $this->form_validation->set_rules('metatitle', 'metatitle', 'xss_clean');
            $this->form_validation->set_rules('metakeyword', 'metakeyword', 'xss_clean');
            $this->form_validation->set_rules('metadescription', 'metadescription', 'xss_clean');
            if ($this->form_validation->run()) {
                $title=$this->input->post('newstitle');
                $description=$this->input->post('newsdescription');
                $metatitle=$this->input->post('metatitle');
                $metakeyword=$this->input->post('metakeyword');
                $metadescription=$this->input->post('metadescription');
                $this->news_model->insert_news($title,$description,$metatitle,$metakeyword,$metadescription);
                $this->session->set_flashdata('message', 'Record Inserted Sucessfully...');
                redirect('admin/news');
            }
                  
             else {
                $this->session->set_flashdata('message', 'News already exists');
                redirect('admin/news');
            }
        }
    }
    function update_news($id)
    {
        $per_page = 10;
        $total = $this->counter_model->news_count();
        $offset=0+$this->uri->segment(5);
        $config['base_url'] = base_url() . 'admin/news/update_news/'.$id.'/';
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '5';
        $this->pagination->initialize($config);
        $data['news']=$this->news_model->get_all_news($per_page, $offset);
        $data['update_news']=$this->news_model->get_news($id);
        $this->load->view('admin/admin_update_news',$data);
        
    }
    function news_update_insert()
    {
       if ($this->input->post('submitnews')) {
            $this->form_validation->set_rules('newstitle', 'newstitle', 'xss_clean');
            $this->form_validation->set_rules('newsdescription', 'newsdescription');
            $this->form_validation->set_rules('metatitle', 'metatitle', 'xss_clean');
            $this->form_validation->set_rules('metakeyword', 'metakeyword', 'xss_clean');
            $this->form_validation->set_rules('metadescription', 'metadescription', 'xss_clean');
            if ($this->form_validation->run()) {
                $id=$this->input->post('id');
                $title=$this->input->post('newstitle');
                $description=$this->input->post('newsdescription');
                $metatitle=$this->input->post('metatitle');
                $metakeyword=$this->input->post('metakeyword');
                $metadescription=$this->input->post('metadescription');
                $this->news_model->update_news($id,$title,$description,$metatitle,$metakeyword,$metadescription);
                $this->session->set_flashdata('message', 'Record Updated Sucessfully...');
                redirect('admin/news');
            }
                  
             else {
                $this->session->set_flashdata('message', 'News already exists');
                redirect('admin/news');
            }
        }
    }
    
    function delete_news($id)
    {
        $this->news_model->delete_news($id);
        redirect('admin/news');
    }
}
?>
